package com.example.rustfs.controller;

import com.example.rustfs.dto.ApiResponse;
import com.example.rustfs.dto.LoginRequest;
import com.example.rustfs.dto.RegisterRequest;
import com.example.rustfs.dto.UserInfo;
import com.example.rustfs.entity.User;
import com.example.rustfs.service.UserService;
import com.example.rustfs.service.FileService;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制器
 */
@Slf4j
@Controller
@RequiredArgsConstructor
public class UserController {
    
    private final UserService userService;
    private final FileService fileService;
    
    /**
     * 登录页面
     */
    @GetMapping("/login")
    public String loginPage() {
        return "login";
    }
    
    /**
     * 注册页面
     */
    @GetMapping("/register")
    public String registerPage() {
        return "register";
    }
    
    /**
     * 登录处理
     */
    @PostMapping("/user/login")
    @ResponseBody
    public ApiResponse<String> login(@RequestBody LoginRequest request, HttpSession session) {
        try {
            User user = userService.login(request.getUsername(), request.getPassword());
            session.setAttribute("user", user);
            session.setAttribute("userId", user.getId());
            
            // 登录时计算并更新存储空间使用量
            long storageUsed = fileService.calculateUserStorageUsed(user.getId());
            userService.updateStorageUsed(user.getId(), storageUsed);
            
            return ApiResponse.success("登录成功");
        } catch (Exception e) {
            log.error("登录失败", e);
            return ApiResponse.error(e.getMessage());
        }
    }
    
    /**
     * 注册处理
     */
    @PostMapping("/user/register")
    @ResponseBody
    public ApiResponse<String> register(@RequestBody RegisterRequest request) {
        try {
            userService.register(request);
            return ApiResponse.success("注册成功，请登录");
        } catch (Exception e) {
            log.error("注册失败", e);
            return ApiResponse.error(e.getMessage());
        }
    }
    
    /**
     * 退出登录
     */
    @GetMapping("/user/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/login";
    }
    
    /**
     * 获取当前用户信息
     */
    @GetMapping("/user/info")
    @ResponseBody
    public ApiResponse<UserInfo> getUserInfo(HttpSession session) {
        Long userId = (Long) session.getAttribute("userId");
        if (userId == null) {
            return ApiResponse.error("未登录");
        }
        
        UserInfo userInfo = userService.getUserInfo(userId);
        return ApiResponse.success(userInfo);
    }
    
    /**
     * 刷新存储空间统计
     */
    @PostMapping("/user/refresh-storage")
    @ResponseBody
    public ApiResponse<UserInfo> refreshStorage(HttpSession session) {
        try {
            Long userId = (Long) session.getAttribute("userId");
            if (userId == null) {
                return ApiResponse.error("未登录");
            }
            
            // 重新计算存储空间
            long storageUsed = fileService.calculateUserStorageUsed(userId);
            userService.updateStorageUsed(userId, storageUsed);
            
            // 返回更新后的用户信息
            UserInfo userInfo = userService.getUserInfo(userId);
            return ApiResponse.success(userInfo);
        } catch (Exception e) {
            log.error("刷新存储空间失败", e);
            return ApiResponse.error(e.getMessage());
        }
    }
}

